package com.hwua.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hwua.entity.Comment;
import com.hwua.entity.PageEntity;
import com.hwua.service.ICommentService;
import com.hwua.service.Impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String param = request.getParameter("param");
        ICommentService commentService = new CommentServiceImpl();
        Map<String,Object> map=new HashMap<>();
        if(param.equals("comm")) {
//分页
            Integer pageNo = Integer.parseInt(request.getParameter("pageNo"));
            Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
            PageEntity pageEntity = new PageEntity();
            try {
                List<Comment> msgList = commentService.queryForLimit((pageNo - 1) * pageSize, pageSize);
                pageEntity.setList2(msgList);
                Long totalRecords = commentService.queryCount();//得到总记录数
                pageEntity.setTotalRecords(totalRecords);
                pageEntity.setPageSize(pageSize);//每页显示的记录数
                pageEntity.setPageNo(pageNo);//当前页
                map.put("comme",pageEntity);
                String pageEntityJson = JSON.toJSONString(map, SerializerFeature.PrettyFormat);
                response.getWriter().write(pageEntityJson);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
        }
        if(param.equals("addNew")){
            String guestName = request.getParameter("guestName");
            String guestTitle = request.getParameter("guestTitle");
            String guestContent = request.getParameter("guestContent");
            Comment comment=new Comment();
            comment.setNick_name(guestName);
            comment.setReply(guestTitle);
            comment.setContent(guestContent);
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String format = dateFormat.format(now);
            comment.setCreate_time(format);
            comment.setReply_time(null);
            try {
                boolean flag = commentService.insertComm(comment);
                if(flag!=true){
                    map.put("success",false);
                    System.out.println("false");
                }else{
                    map.put("success",true);
                    System.out.println("true");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            response.getWriter().write( JSON.toJSONString(map));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
